package top.bingoz.wanandroid

import org.junit.Assert.assertEquals
import org.junit.Test
import retrofit2.Retrofit
import retrofit2.mock.MockRetrofit
import retrofit2.mock.NetworkBehavior
import top.bingoz.wanandroid.data.http.coverter.FastJsonConverterFactory
import top.bingoz.wanandroid.test.ApiMockService
import top.bingoz.wanandroid.test.ApiService
import java.util.concurrent.TimeUnit

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }

    @Test
    fun testRxjava(): Unit {
    }


    @Test
    fun testApi() {
        // Create a very simple REST adapter which points the Api API.
        val retrofit = Retrofit.Builder()
                .baseUrl(ApiService.API_URL)
//                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(FastJsonConverterFactory.create())
                .build()

        // Create an instance of our Api API interface.
        val test = retrofit.create(ApiService.Api::class.java)

        // Create a call instance for looking up Retrofit contributors.
        val call = test.getUserInfo()

        // Fetch and print a list of the contributors to the library.
        val bean = call.execute().body()
        println("isAgent: ${bean?.isAgent}")
    }

    @Test
    fun testMockApi() {
        // Create a very simple Retrofit adapter which points the Api API.
        val retrofit = Retrofit.Builder()
                .baseUrl(ApiService.API_URL)
                .build()

        // Create a MockRetrofit object with a NetworkBehavior which manages the fake behavior of calls.
        val behavior = NetworkBehavior.create()
        val mockRetrofit = MockRetrofit.Builder(retrofit)
                .networkBehavior(behavior)
                .build()

        val delegate = mockRetrofit.create(ApiService.Api::class.java)
        val test = ApiMockService.MockApi(delegate)

        // Query for some contributors for a few repositories.

        // Using the mock-only methods, add some additional data.
//        println("Adding more mock data...\n")

        // Reduce the delay to make the next calls complete faster.
        behavior.setDelay(500, TimeUnit.MILLISECONDS)
        // Fetch and print a list of the contributors to the library.
        val call = test.getUserInfo()
        val bean = call.execute().body()
        println("isAgent: ${bean?.isAgent}")
        // Query for the contributors again so we can see the mock data that was added.
    }
}
